package com.card.manager.services.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.card.framework.mybatis.mapper.MyFrameworkSupperMapper;
import com.card.manager.dao.ResourcesMapper;
import com.card.manager.domain.Resources;
import com.card.manager.domain.ResourcesExample;
import com.card.manager.services.ResourcesService;
@Service("resourceServiceImpl")
public class ResourcesServiceImpl extends MyFrameworkSupperMapper implements ResourcesService {

	@Override
	public List<Resources> getAllEnabledResources() {
		ResourcesMapper mapper = this.getMapper(ResourcesMapper.class);
		
		ResourcesExample example = new ResourcesExample();
		example.createCriteria().andDelflgEqualTo((byte)1).andEnableEqualTo((byte)1);
		
		return mapper.selectByExample(example);
	}

	@Override
	public Resources getById(int id) {
		ResourcesMapper mapper = this.getMapper(ResourcesMapper.class);
		return mapper.selectByPrimaryKey(id);
	}
	
	@Override
	@Transactional
	public void add(Resources resource) {
		ResourcesMapper mapper = this.getMapper(ResourcesMapper.class);
		Resources parent = mapper.selectByPrimaryKey(resource.getParentid());
		resource.setIdpath(parent.getIdpath() + parent.getResouceid() + ".");
		mapper.insert(resource);
	}
	@Override
	@Transactional
	public void update(Resources resource) {
		ResourcesMapper mapper = this.getMapper(ResourcesMapper.class);
		Resources parent = mapper.selectByPrimaryKey(resource.getParentid());
		String idpath = parent.getIdpath() + parent.getResouceid() + ".";
		if(idpath.equals(resource.getIdpath())){
			ResourcesExample example = new ResourcesExample();
			example.createCriteria().andResouceidEqualTo(resource.getResouceid());
			mapper.updateByExampleSelective(resource, example);
		}else{
			ResourcesExample example = new ResourcesExample();
			String oidpath = resource.getIdpath();
			
			resource.setIdpath(idpath);
			mapper.updateByPrimaryKey(resource);
			
			Resources update = new Resources();
			update.setIdpath(idpath + resource.getResouceid() + ".");
			example.createCriteria().andIdpathLike(oidpath + resource.getResouceid() + ".%");
			mapper.updateByExampleSelective(update, example);
			
		}
	}
	@Transactional
	public void updateIdPath() {
		ResourcesMapper mapper = this.getMapper(ResourcesMapper.class);
		ResourcesExample example = new ResourcesExample();
		example.createCriteria().andParentidEqualTo(-2);
		List<Resources> list = mapper.selectByExample(example);
		for(Resources r : list){
			r.setIdpath(".-2.");
			mapper.updateByPrimaryKey(r);
			_updateChildrenIdPath(r.getResouceid());
		}
	}
	private void _updateChildrenIdPath(int parentId){
		ResourcesMapper mapper = this.getMapper(ResourcesMapper.class);
		ResourcesExample example = new ResourcesExample();
		example.createCriteria().andParentidEqualTo(parentId);
		List<Resources> list = mapper.selectByExample(example);
		Resources parent = mapper.selectByPrimaryKey(parentId);
		for(Resources r : list){
			r.setIdpath(parent.getIdpath() + parent.getResouceid() + ".");
			mapper.updateByPrimaryKey(r);
			_updateChildrenIdPath(r.getResouceid());
		}
	}

	@Override
	@Transactional
	public void delete(Integer id) {
		ResourcesMapper mapper = this.getMapper(ResourcesMapper.class);
		Resources resource = mapper.selectByPrimaryKey(id);
		if(resource != null){
			ResourcesExample example = new ResourcesExample();
			example.createCriteria().andIdpathLike(resource.getIdpath() + resource.getResouceid() + ".%");
			Resources updateChildren = new Resources();
			updateChildren.setDelflg((byte)0);
			mapper.updateByExampleSelective(updateChildren,example);
			resource.setDelflg((byte)0);
			mapper.updateByPrimaryKey(resource);
		}
	}

	@Override
	public List<Resources> getAllEnabledResourcesNotInclude(int id) {
		ResourcesMapper mapper = this.getMapper(ResourcesMapper.class);
		
		Resources resource = mapper.selectByPrimaryKey(id);
		ResourcesExample example = new ResourcesExample();
		example.createCriteria()
			.andDelflgEqualTo((byte)1)
			.andEnableEqualTo((byte)1)
			.andIdpathNotLike(resource.getIdpath() + resource.getResouceid() + ".%")
			.andResouceidNotEqualTo(id);
		
		return mapper.selectByExample(example);
	}
}
